package com.zrrd.yunchmall.sale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.sale.entity.HomeNewProduct;
import com.zrrd.yunchmall.sale.entity.HomeRecommendProduct;
import com.zrrd.yunchmall.sale.entity.HomeRecommendSubject;
import com.zrrd.yunchmall.sale.service.IHomeRecommendProductService;
import com.zrrd.yunchmall.sale.service.IHomeRecommendSubjectService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 首页推荐专题表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/home/recommendSubject")
@Api(tags = "营销服务-专题推荐模块")
public class HomeRecommendSubjectController {
    @Autowired
    private IHomeRecommendSubjectService productService;

    @GetMapping("/list")
    @ApiOperation("专题推荐模块")
    private ResponseResult list(@RequestParam int pageSize, @RequestParam int pageNum, String subjectName){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(subjectName)){
            queryWrapper.like("subject_name",subjectName);
        }
        Page<HomeRecommendSubject> pageInfo=productService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return new ResponseResult<>(200,"查询成功!",pageInfo);
    }
    @PostMapping("/delete")
    @ApiOperation("删除专题推荐")
    private ResponseResult delete(@RequestParam String ids){
        for (String id:ids.split(",")) {
            productService.removeById(Long.valueOf(id));
        }
        return new ResponseResult<>(200,"删除成功!");
    }
    @PostMapping("/create")
    @ApiOperation("添加专题推荐")
    private ResponseResult create(@RequestBody List<HomeRecommendSubject> homeNewProducts){
        homeNewProducts.forEach(homeNewProduct -> {
            QueryWrapper queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("brand_id",homeNewProduct.getId());
            if (productService.count(queryWrapper)==0){
                homeNewProduct.setRecommendStatus(1);
                productService.save(homeNewProduct);
            }
        });
        return new ResponseResult<>(200,"添加成功!");
    }
    @PostMapping("/update/recommendStatus")
    @ApiOperation("修改推荐状态")
    public ResponseResult updateRecommendStatus(String ids,Integer recommendStatus){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("recommend_status",recommendStatus);
        updateWrapper.in("id",ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }
}
